package com.res.simulator.handler;

import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;

import com.res.simulator.model.SelectTemplateModel;
import com.res.simulator.plugins.MessageCommunicationClientPlugin;
import com.res.simulator.transformer.ConsumeTemplateSearchTransformer;
import com.res.simulator.view.ConsumerTemplateSearchResultsView;
import com.res.system.utils.SimulatorUtilities;
import com.res.system.utils.SimulatorUtilities.ConfigData;
import com.sip.comserver.communication.CommunicationMessage;
import com.sip.dbadapter.dao.domain.ConsumeTemplate;

public class SelectApiHandler {

    private static final Logger logger = Logger.getLogger(SelectApiHandler.class);

    public List<ConsumerTemplateSearchResultsView> process(SelectTemplateModel model) throws Exception {

        // TODO Auto-generated method stub
        final ConsumeTemplateSearchTransformer apiSearchTransformer = new ConsumeTemplateSearchTransformer();

        List<ConsumeTemplate> consumeTemplateList = apiSearchTransformer.transformModelToDomian(model);

        List<ConsumerTemplateSearchResultsView> searchViewList = null;

        if ((null != model.getApiId() && model.getApiId().trim().length() != 0)
            || (null != model.getApiName() && model.getApiName().trim().length() != 0)) {

            consumeTemplateList = commit(consumeTemplateList.get(0));
            searchViewList = apiSearchTransformer.transformDomianToView(consumeTemplateList);

        }

        return searchViewList;
    }

    /**
     * Commited data to back end and return message will be directed to
     * 
     * @param domainCollection
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private List<ConsumeTemplate> commit(ConsumeTemplate consumeTemplate) throws IOException, ClassNotFoundException {

        final CommunicationMessage message = new CommunicationMessage();

        final String serverAddress = SimulatorUtilities.getConfigData(ConfigData.SERVER_ADDRESS);
        final String port = SimulatorUtilities.getConfigData(ConfigData.SERVER_PORT);

        logger.info("Message Header==>" + "JOBType:cm" + "|ID:" + serverAddress + "|Port:" + port + "|Executor:TemplateSelection");

        message.setMessageHeader("JOBType:GenericAPIExecution|Executor:TemplateSelection|ID:" + serverAddress + "|Port:" + port);

        message.setObject(consumeTemplate);

        final MessageCommunicationClientPlugin plugin = new MessageCommunicationClientPlugin();
        final CommunicationMessage returnedMessage = plugin.invoke(message);
        final List<ConsumeTemplate> listTemplate = (List<ConsumeTemplate>) returnedMessage.getObject();
        if (listTemplate.size() != 0)
            for (ConsumeTemplate consumeObject : listTemplate)
                System.out.println(">>>>>>>>>>>>> content " + consumeObject.getContent() + "template id " + consumeObject.getTemplateId()
                    + " save Date " + consumeObject.getSavedDate()+"apiName "+consumeObject.getApiName());
        
        return listTemplate;
    }

}
